"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.responseDownloadFile = void 0;
const mime = require('./mime');
const fs = require('fs');
const path = require('path');
function responseDownloadFile(res, downloadFilePath, downloadFileName) {
    if (!fs.existsSync(downloadFilePath)) {
        res.writeHead(404, { "Content-Type": "text/html" });
        res.write("404 :Can not found this Page !");
        res.end();
        return;
    }
    downloadFileName = downloadFileName ? downloadFileName : path.basename(downloadFilePath);
    let ext = path.extname(downloadFileName);
    ext = ext ? ext.slice(1) : 'unknown';
    let contentType = mime[ext] || 'text/plain';
    res.writeHead(200, {
        'Content-Type': contentType,
        'content-length': fs.statSync(downloadFilePath).size,
        'Content-Disposition': ('attachment;filename=' + encodeURI(downloadFileName))
    });
    let stream = fs.createReadStream(downloadFilePath);
    stream.pipe(res);
}
exports.responseDownloadFile = responseDownloadFile;
